<?php

namespace Admin\Controller;

use Think\Controller;


class RbacController extends CommonController
{
    //添加角色
    public function addRole()
    {
        $this->display();
    }

    //添加角色表单处理
    public function addRoleHandle()
    {
        $data = I('post.');
        $res = M('role')->add($data);

        if ($res) {
            json('操作成功', 0, '');
        } else {
            json('操作失败', 1, '');
        }
    }

    //角色管理(角色列表)
    public function roleList()
    {
        $this->display();
    }
    public function roleListApi()
    {
        $res = M('role')->select();
        if ($res) {
            json('操作成功', 0, $res);
        } else {
            json('操作失败', 1, '');
        }
    }

    public function setstatus()
    {
        $data = I('post.');
        $where['id'] = $data['id'];
        $res = M('role')->where($where)->save($data);
        if ($res) {
            json('操作成功', 0, $res);
        } else {
            json('操作失败', 1, '');
        }
    }


    //添加权限(节点)
    public function addNode()
    {
        $this->node = M('node')->where("level != 3")->order('sort')->select();
        $this->display();
    }

    //添加节点表单处理
    public function addNodeHandle()
    {
        $data = I('post.');
        $res = M('node')->add($data);
        if ($res) {
            json('操作成功', 0, '');
        } else {
            json('操作失败', 1, '');
        }
    }

    //权限管理(节点列表)
    public function nodeList()
    {
        $this->display();
    }
    public function nodeListApi()
    {
        import('ORG.Util.Tree');
        $node = M('node')->order('sort')->select();
        $res = \Tree::create($node);
        if ($res) {
            json('操作成功', 0, $res);
        } else {
            json('操作失败', 1, '');
        }
    }

    //删除权限(节点)
    public function deleteNode()
    {
        $id = I('get.id', '', 'trim');
        $where['id'] = $id;
        $res = M('node')->where($where)->delete();
        if ($res) {
            json('操作成功', 0, $res);
        } else {
            json('操作失败', 1, '');
        }
    }

    //添加用户
    public function addUser()
    {
        $this->role = M('role')->select();
        $this->display();
    }

    //添加用户处理
    public function addUserHandle()
    {
        $data['username'] = I('post.username');
        $data['password'] = I('post.password');
        $data['logintime'] = time();
        $data['loginip'] = get_client_ip();
        $data['status'] = 1;

        $uid = M('user')->add($data);   //对应第几条
        if ($uid) {

            //用户添加成功后添加用户角色表
            $role['role_id'] = I('post.role_id');
            $role['user_id'] = $uid;

            $res = M('role_user')->add($role);
            if ($res) {
                json('添加成功', 0, '');
            } else {
                json('添加失败', 1, '');
            }
        }
    }

    //用户列表
    public function userList()
    {
        $this->display();
    }
    //用户列表
    public function userListApi()
    {
        $res = D('user')->field('password', true)->relation(true)->select();  //模型，管理表，多对多关联
        if ($res) {
            json('操作成功', 0, $res);
        } else {
            json('操作失败', 1, $res);
        }
    }

    // //配置权限
    public function access()
    {
        import('ORG.Util.Tree');
        $rid = I('get.rid');
        $node = M('node')->order('sort')->select();
        $node = \Tree::create($node);

        $data = array(); //存放最新数组，里面是否包含当前用户组每一权限
        $access = M('access');
        foreach ($node as $value) {
            $count = $access->where("role_id = '{$rid}' and node_id = '{$value['id']}'")->count();
            if ($count) {
                $value['access'] = 1;
                $value['checked'] = true;
            } else {
                $value['access'] = 0;
                $value['checked'] = false;
            }
            $data[] = $value;
        }

        $this->nodelist = $data;
        $this->rid = $rid;
        $this->name = M('role')->getFieldById($rid, 'name');
        $this->display();
    }

    //配置权限接口
    public function treeList()
    {
        $rid = I('get.rid');
        if ($rid) {
            import('ORG.Util.Tree');
            $node = M('node')->order('sort')->select();
            $node = \Tree::create($node);

            $data = array(); //存放最新数组，里面是否包含当前用户组每一权限
            $access = M('access');
            foreach ($node as $value) {
                $count = $access->where("role_id = '{$rid}' and node_id = '{$value['id']}'")->count();
                if ($count) {
                    $value['access'] = 1;
                    $value['checked'] = true;
                    $value['value'] = $value['id'] . '_' . $value['level'];
                } else {
                    $value['access'] = 0;
                    $value['checked'] = false;
                    $value['value'] = $value['id'] . '_' . $value['level'];
                }
                $data[] = $value;
            }
            $data = generateTree($data);
            if ($data) {
                $list['code'] = 0;
                $list['msg'] = '获取数据成功';
                $list['data']['trees'] = $data;
                $this->ajaxReturn($list);
            } else {
                $list['code'] = 1;
                $list['msg'] = '获取数据失败';
                $list['data']['trees'] = '';
                $this->ajaxReturn($list);
            }
        }
    }

    //添加角色-权限表(节点表)
    public function setAccess()
    {
        $rid = I('post.rid');
        $access = M('access')->where("role_id = '{$rid}'");
        $access->delete();

        if (isset($_POST['access'])) {
            $data = array();
            foreach ($_POST['access'] as $value) {
                $tmp = explode('_', $value);  //拆分数组
                $data[] = array(
                    'role_id' => $rid,
                    'node_id' => $tmp[0],
                    'level' => $tmp[1]
                );
            }
            if ($access->addAll($data)) {
                $this->success('添加成功', U('Rbac/addRole'));
            } else {
                $this->error('添加失败', U('Rbac/addRole'));
            }
        } else {
            $this->error('修改成功');
        }
    }
}
